package com.blog.listener;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.blog.entity.User;
import com.blog.service.UserService;

public class FileUploadUtil {
	
	@Autowired UserService userService;
	
	public static Map<String,String> upLoadFile(HttpServletRequest request, CommonsMultipartFile file) {
		Map<String,String> resMsg = new HashMap<String, String>();
		String upRes = "";
		String picPath = "";
		String path = request.getSession().getServletContext().getRealPath("assets/images");
		String fileName = file.getOriginalFilename();
		picPath = "assets/images/" + fileName;
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		try {
			file.transferTo(targetFile);
			upRes = "true";
			resMsg.put("upRes", upRes);
			resMsg.put("picPath", picPath);
			return resMsg;
		} catch (Exception e) {
			e.printStackTrace();
			upRes = "false";
			resMsg.put("upRes", upRes);
			return resMsg;
		}
	}
}
